package com.bbs.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.bbs.core.common.WebUtils;
import com.bbs.core.model.Post;
import com.bbs.core.model.Topic;
import com.bbs.core.model.User;
import com.bbs.core.service.PostService;
import com.bbs.core.service.ReplyService;
import com.bbs.core.service.TopicService;
import com.github.pagehelper.PageHelper;

@Controller
public class AdminController {
	
	@Autowired
	private TopicService topicService;
	
	@Autowired
	private PostService postService;
	
	@Autowired
	private ReplyService replyService;
	
	@Autowired
	private WebUtils webUtils;
	
	@ResponseBody
	@PostMapping("/bbs/admin/topic/nice/{id}")
	public JSONObject editNiceTopic(@PathVariable int id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		if(!webUtils.isAdmin(request, response)){
			//如果有非法使用，不提示具体信息，直接返回null
			result.put("err", 1);
			result.put("msg", "呵呵~~");
		}else{
			Topic db = topicService.getTopicByPrimaryKey(id);
			db.setIsNice("1");
			topicService.updateTopic(db);
			result.put("err", 0);
			result.put("msg", "success");
		}
		return result;
	}
	
	@ResponseBody
	@PostMapping("/bbs/admin/topic/up/{id}")
	public JSONObject editUpTopic(@PathVariable int id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		if(!webUtils.isAdmin(request, response)){
			//如果有非法使用，不提示具体信息，直接返回null
			result.put("err", 1);
			result.put("msg", "呵呵~~");
		}else{
			Topic db = topicService.getTopicByPrimaryKey(id);
			db.setIsUp("1");
			topicService.updateTopic(db);
			result.put("err", 0);
			result.put("msg", "success");
		}
		return result;
	}

	
	@ResponseBody
	@PostMapping("/bbs/admin/topic/delete/{id}")
	public JSONObject deleteTopic(@PathVariable int id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		if(!webUtils.isAdmin(request, response)){
			//如果有非法使用，不提示具体信息，直接返回null
			result.put("err", 1);
			result.put("msg", "呵呵~~");
		}else{
			topicService.deleteTopic(id);
			result.put("err", 0);
			result.put("msg", "success");
		}
		return result;
	}

	@RequestMapping("/bbs/admin/post/{p}")
	public ModelAndView adminPosts(ModelAndView view, @PathVariable int p){
		view.setViewName("/bbs/admin/postList.html");
		PageHelper.startPage(1, 6);
		List<Post> findAllPosts = postService.findAllPosts();
		view.addObject("postPage", findAllPosts);
		return view;
	}

	@RequestMapping("/bbs/admin/post/edit/{id}.html")
	public ModelAndView editPost(ModelAndView view, @PathVariable int id,HttpServletRequest request, HttpServletResponse response){
		view.setViewName("/postEdit.html");
		Post post = postService.selectByPrimaryKey(id);
		Topic topic = topicService.getTopicByPrimaryKey(post.getTopicId());
		view.addObject("post",post );
		view.addObject("topic", topic);
		return view;
	}

	/**
	 * ajax方式编辑内容
	 * @param view
	 * @param post
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/bbs/admin/post/update")
	public JSONObject updatePost(ModelAndView view, Post post,HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		result.put("err", 1);
		if(post.getContent().length()<10){
			result.put("msg", "输入的内容太短，请重新编辑！");
		}else{
			Post db = postService.selectByPrimaryKey(post.getId());
			if(canUpdatePost(db,request,response)){
				db.setContent(post.getContent());
				postService.updateByPrimaryKeySelective(db);;
				result.put("msg", "/bbs/topic/"+db.getTopicId()+"-1.html");
				result.put("err", 0);
			}else{
				result.put("msg", "不是自己发表的内容无法编辑！");
			}
		}
		return result;
	}

	/**
	 * ajax方式删除内容
	 * @param view
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/bbs/admin/post/delete/{id}")
	public JSONObject deletePost(ModelAndView view, @PathVariable int id,HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Post post = postService.selectByPrimaryKey(id);
		if(canUpdatePost(post,request,response)){
			postService.deletePost(id);;
			result.put("err", 0);
			result.put("msg", "删除成功！");
		}else{
			result.put("err", 1);
			result.put("msg", "不是自己发表的内容无法删除！");
		}
		return result;
	}

	

	@RequestMapping("/bbs/admin/reply/delete/{id}")
	public ModelAndView deleteReply(ModelAndView view, @PathVariable int id){
		replyService.deleteByPrimaryKey(id);
		view.setViewName( "forward:/bbs/admin/reply/1");
		return view;
	}
	
	private boolean canUpdatePost(Post post,HttpServletRequest request, HttpServletResponse response){
		User user = this.webUtils.currentUser(request, response);
		if(post.getUserId().equals(user.getId())){
			return true ;
		}
		//如果是admin
		if(user.getUserName().equals("admin")){
			return true;
		}
		
		return false;
	}

}
